import { defineStore } from 'pinia';

export const useLoginStore = defineStore('login', {
    state: () => ({
        loginInfo: null as Record<string, any> | null,
    }),

    getters: {
        getLoginInfo: (state) => {
            if (!state.loginInfo) {
                const storedInfo = localStorage.getItem('loginInfo');
                if (storedInfo) {
                    state.loginInfo = JSON.parse(storedInfo);
                }
            }
            return state.loginInfo;
        },
    },

    actions: {
        // 设置登录信息
        setLoginInfo(info: Record<string, any>) {
            this.loginInfo = info;
        },
        // 清除登录信息
        clearLoginInfo() {
            this.loginInfo = null;
            localStorage.removeItem('loginInfo');
        },

    },

    persist: {
        enabled: true,
        strategies: [
            {
                storage: localStorage,
            },
        ],
    },
});